package com.xunlai.infra.permission.business.controller;

import com.xunlai.infra.permission.business.service.RouteService;
import com.xunlai.infra.permission.business.vo.route.*;
import com.xunlai.infra.permission.common.security.TenantUser;
import com.xunlai.infra.permission.common.vo.BasePageRespVo;
import com.xunlai.infra.permission.common.vo.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liang
 * @date 8/16/23 9:57 PM
 */
@RestController
@RequestMapping("travel/manage/route/template")
public class RouteTemplateManageController {

    private RouteService routeService;

    @PostMapping("create")
    public BaseResponse<Long> createRouteType(@RequestBody RouteTemplateCreateVo routeTemplateCreateVo) {
        return BaseResponse.success(routeService.createRouteTemplate(routeTemplateCreateVo));
    }

    @GetMapping("list")
    public BaseResponse<BasePageRespVo<RouteTemplateListVo>> listRouteTemplate(RouteTemplateQueryVo routeTemplateQueryVo,
                                                                           @RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                                                           @RequestParam(required = false,defaultValue = "10") Integer pageSize) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if(principal instanceof TenantUser tenantUser) {
            String id = tenantUser.getId();
            return BaseResponse.success(routeService.listRouteTemplate(Long.valueOf(id),routeTemplateQueryVo,pageNum,pageSize));
        }else {
            return BaseResponse.success(routeService.listRouteTemplate(routeTemplateQueryVo,pageNum,pageSize));
        }
    }

    @GetMapping("all")
    public BaseResponse<List<RouteTemplateListVo>> listAllRouteTemplate(RouteTemplateQueryVo routeTemplateQueryVo) {
        return BaseResponse.success(routeService.listAllRouteTemplate());
    }

    @PutMapping("update/{id}")
    public BaseResponse<Boolean> updateRouteTemplate(@PathVariable Long id ,@RequestBody RouteTemplateCreateVo routeTemplateCreateVo){
        routeService.updateRouteTemplate(id,routeTemplateCreateVo);
        return BaseResponse.success();
    }


    @Autowired
    public void setRouteService(RouteService routeService) {
        this.routeService = routeService;
    }
}
